package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.SurfaceResize;
	import com.manager.Vision;
	import com.utils.PowerLoader;
	
	import feathers.controls.Label;
	import feathers.controls.ProgressBar;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.utils.setTimeout;
	
	import starling.display.DisplayObjectContainer;
	import starling.display.Image;
	import starling.display.Sprite;

	public class LoadingUI extends SurfaceResize
	{
		private static var loadUI:LoadingUI;
		public static function getInstance():LoadingUI{
			if(loadUI == null)loadUI = new LoadingUI();
			return loadUI;
		}
		
		public function show(x:Number = 0,y:Number = 0):void{
			initFace();
			addListeners();
			initSize();
		}
		
		private function diappear():void{
			Vision.getLayer(Vision.UI).visible = true;
			loadView.visible = false;
			AwayEngine.cameraEnabled = true;
		}
		private function vision():void{
			Vision.getLayer(Vision.UI).visible = false;
			loadView.visible = true;
			AwayEngine.cameraEnabled = false;
		}
		
		
		protected override function resize():void{
			back.width = Math.round(AwayEngine.senceWidth);
			back.height = Math.round(AwayEngine.senceHeight);
			back.x = Math.round(-back.width / 2);
			back.y = Math.round(-back.height / 2);
			loadView.x = AwayEngine.senceWidth / 2;
			loadView.y = AwayEngine.senceHeight / 2;
		}
		
		private var loadView:Sprite;
		private var progress:ProgressBar;//进度条
		private var label:Label;//显示进度信息
		private var back:Image;
		private function initFace():void
		{
			if(loadView == null){
				loadView = new Sprite();
				
				back = CustomSkin.introduce(
					AwayEngine.senceWidth,
					AwayEngine.senceHeight,
				0,0,0x272320);
				loadView.addChild(back);
				
				progress = new ProgressBar();
				progress.minimum = 0;
				progress.maximum = 100;
				loadView.addChild(progress);
				progress.width = 400;
				progress.height = 30;
				progress.value = 40;//显示进度比例
				
				progress.paddingTop = 2;//和左右上下边距
				progress.paddingRight = 3;
				progress.paddingBottom = 2;
				progress.paddingLeft = 3;
				
				progress.x = - progress.width / 2;
				progress.y = - progress.height / 2;
				
				label = new Label();
				label.width = 50;
				label.height = 20;
				loadView.addChild(label);
				label.x = -label.width / 2;
				label.y = progress.y - label.height - 5;
			}
			Vision.addView(Vision.TIPS,loadView);
			resize();
		}
		
		private function addListeners():void
		{
			loadView.visible = false;
			PowerLoader.addProgressEvent(loadProgress);
			PowerLoader.addCompleteEvent(loadComplete);
		}
		
		private function loadComplete(e:Event):void
		{
			diappear();
		}
		
		private function loadProgress(e:ProgressEvent):void
		{
			if(!loadView.visible)vision();
			var loadPhase:int = e.bytesLoaded / e.bytesTotal * 100;
			label.text = loadPhase + '%';
			progress.value = loadPhase;
		}
	}
}